package com.db;

import com.db.DbConnection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Save {
	public int savePath(int userid, String pathname, int state,
			String description) {
		String sql = "insert into all_path(userid,pathname,state,description) values ('"
				+ userid
				+ "','"
				+ pathname
				+ "','"
				+ state
				+ "','"
				+ description + "')";
		String sql1 = "select id from all_path where pathname='" + pathname
				+ "' and userid='" + userid + "'";
		Connection con = null;
		int result = 0;
		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();
			if (st.executeQuery(sql1).next()) {
				return result;// 存在同名的时候
			}
			result = st.executeUpdate(sql);
			sql = "select id from all_path where pathname='" + pathname + "'";
			ResultSet rs = st.executeQuery(sql);
			if (rs.next()) {
				result = rs.getInt("id");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
	public int modifyPath(String pathid, String pathname, int state,
			String description) {
		
		String sql="update all_path set pathname='"+pathname+"' ,state='"+state+"' ,description='"+description+
				"' where id='" +pathid+"'";
		
		String sql1 = "select id from all_path where id='" + pathid+ "'";
		Connection con = null;
		int result = 0;
		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();
			if (!st.executeQuery(sql1).next()) {
				return result;// 路线不存在时
			}
			result = st.executeUpdate(sql);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	public int saveRoute(String pathid, int slat, int slng, int elat, int elng,
			String num) {
		String sql = "insert into route(pathid,slatitude,slongitude,elatitude,elongitude,pathnum) values ('"
				+ pathid
				+ "','"
				+ slat
				+ "','"
				+ slng
				+ "','"
				+ elat
				+ "','"
				+ elng
				+ "','"
				+num
				+ "')";
		
		Connection con = null;
		int result = 0;
		try {
			con = DbConnection.getInstance().getConnection();

			Statement st = con.createStatement();
			
			result = st.executeUpdate(sql);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

}
